package com.hruiworks.campusgroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hruiworks.campusgroup.common.PageVO;
import com.hruiworks.campusgroup.enums.ErrorCode;
import com.hruiworks.campusgroup.exception.BusinessException;
import com.hruiworks.campusgroup.pojo.entity.ActivityGrade;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.service.ActivityGradeService;
import com.hruiworks.campusgroup.dao.mapper.ActivityGradeMapper;
import com.hruiworks.usercheck.util.ParamUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
* @author jiushui
* @description 针对表【activity_grade(活动评分)】的数据库操作Service实现
* @createDate 2025-03-28 18:08:00
*/
@Service
public class ActivityGradeServiceImpl extends ServiceImpl<ActivityGradeMapper, ActivityGrade>
    implements ActivityGradeService{

    @Resource
    private ActivityGradeMapper activityGradeMapper;

    @Override
    public Boolean submitGrade(Long activityId, Integer grade, String comment, User user) {
        if (ParamUtils.isAnyBlank(activityId, grade, comment)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数有误或不全");
        }
        ActivityGrade activityGrade = new ActivityGrade();
        activityGrade.setUserId(user.getId());
        activityGrade.setActivityId(activityId);
        activityGrade.setGrade(grade);
        activityGrade.setComment(comment);
        int insert = activityGradeMapper.insert(activityGrade);
        if (insert != 1) {
            throw new BusinessException(ErrorCode.OTHER_ERROR, "提交评分失败");
        }
        return true;
    }

    @Override
    public ActivityGrade getSelfGrade(Long activityId, User user) {
        LambdaQueryWrapper<ActivityGrade> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActivityGrade::getActivityId, activityId);
        lambdaQueryWrapper.eq(ActivityGrade::getUserId, user.getId());
        return activityGradeMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public Boolean updateSelfGrade(Long activityId, Integer grade, String comment, User user) {
        if (ParamUtils.isAnyBlank(activityId, grade, comment)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数有误或不全");
        }

        LambdaQueryWrapper<ActivityGrade> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActivityGrade::getActivityId, activityId);
        lambdaQueryWrapper.eq(ActivityGrade::getUserId, user.getId());

        ActivityGrade activityGrade = new ActivityGrade();
        activityGrade.setGrade(grade);
        activityGrade.setComment(comment);

        int update = activityGradeMapper.update(activityGrade, lambdaQueryWrapper);

        if (update != 1) {
            throw new BusinessException(ErrorCode.OTHER_ERROR, "更新评分失败");
        }
        return true;
    }

    @Override
    public PageVO<ActivityGrade> pageActivityGrade(Long current, Long pageSize, Long activityId) {
        if (ParamUtils.isAnyBlank(activityId, current, pageSize)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数有误或不全");
        }

        PageVO<ActivityGrade> activityGradePageVO = new PageVO<>();
//        activityGradePageVO.setTotal(0L);
//        activityGradePageVO.setSize(pageSize);
//        activityGradePageVO.setCurrent(current);

        Page<ActivityGrade> queryActivityGradePage = new Page<>(current, pageSize);
        LambdaQueryWrapper<ActivityGrade> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActivityGrade::getActivityId, activityId);
        lambdaQueryWrapper.orderByDesc(ActivityGrade::getId);

        Page<ActivityGrade> activityGradePage = activityGradeMapper.selectPage(queryActivityGradePage, lambdaQueryWrapper);

//        if (activityGradePage.getRecords().isEmpty()) {
//            activityGradePageVO.setRecords(Collections.emptyList());
//            return activityGradePageVO;
//        }


        activityGradePageVO.setRecords(activityGradePage.getRecords());
        activityGradePageVO.setTotal(activityGradePage.getTotal());
        activityGradePageVO.setSize(activityGradePage.getSize());
        activityGradePageVO.setCurrent(activityGradePage.getCurrent());

        return activityGradePageVO;
    }

    @Override
    public Long activityCommentCount(Long activityId) {
        LambdaQueryWrapper<ActivityGrade> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActivityGrade::getActivityId, activityId);
        return activityGradeMapper.selectCount(lambdaQueryWrapper);
    }

}




